package com.ian.controller;

import com.common.util.ResultMsg;
import com.ian.pojo.PerCar;
import com.ian.service.PerCarService;
import com.ian.service.PerUserService;
import com.ian.vo.KeyBoardVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 车辆服务
 *
 * @Author IanZy
 * @Date 2020/6/18
 */
@Api("车辆相关的API")
@RestController
public class PerCarController {
    @Autowired
    PerUserService perUserService;

    @Autowired
    PerCarService perCarService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 车辆信息列表
     *
     * @Author IanZy
     * @Date 2020/6/20
     */
    @ApiOperation("查询用户车辆信息")
    @GetMapping(value = "/cars", produces = "application/json;charset=UTF-8")
    public ResultMsg cars(@RequestParam("mobile") String mobile) {
        if (mobile != null) {
            List<PerCar> cars = perCarService.findCarsByUid(perUserService.findByTel(mobile).getPerUserId());
            return ResultMsg.success("查询车辆信息成功~", cars);
        }
        return ResultMsg.failed("查询车辆信息失败~");
    }

    /**
     * 删除车辆信息
     *
     * @Author IanZy
     * @Date 2020/6/20
     */
    @ApiOperation("删除车辆信息")
    @DeleteMapping(value = "/deleteCar", produces = "application/json;charset=UTF-8")
    public ResultMsg deleteCar(@RequestParam("id") int id) {
        if (id != 0) {
            perCarService.deleteByPrimaryKey(id);
            return ResultMsg.success("删除车辆成功~");
        }
        return ResultMsg.failed("删除车辆失败~");
    }

    @ApiOperation("输入车牌号")
    @PostMapping(value = "/keyboard", produces = "application/json;charset=UTF-8")
    public ResultMsg keyboard(@RequestBody List<KeyBoardVo> map) {
        String str = "";
        for (KeyBoardVo vo : map) {
            if (vo.getVal() != null) {
                str += vo.getVal();
            }
        }
        System.out.println(str);
        redisTemplate.opsForValue().set("keyboard", str);
        redisTemplate.expire("keyboard", 30, TimeUnit.SECONDS);
        return ResultMsg.success("将车牌号存入redis成功~", str);
    }

    @ApiOperation("添加车牌信息")
    @PostMapping(value = "/insertCarNum", produces = "application/json;charset=UTF-8")
    public ResultMsg insertCarNum(@RequestParam("mobile") String mobile) {
        PerCar perCar = new PerCar();
        perCar.setPerUserId(perUserService.findByTel(mobile).getPerUserId());
        perCar.setPerCarAttribute(0);
        perCar.setPerCarPlatenum(redisTemplate.opsForValue().get("keyboard").toString());
        perCar.setPerCarImg("https://car3.autoimg.cn/cardfs/product/g3/M04/1B/40/240x180_0_q95_c42_autohomecar__ChsEm14Jp9iATIMeAAd-4uKWT0A908.jpg");
        perCarService.insert(perCar);
        return ResultMsg.success("添加车牌成功~", mobile);
    }

    @ApiOperation("上传车辆图片")
    @PutMapping(value = "/uploadCarImg", produces = "application/json;charset=UTF-8")
    public ResultMsg uploadCarImg(@RequestParam("path") String path, @RequestParam("carnum") String carnum) {
        PerCar perCar = new PerCar();
        perCar.setPerCarPlatenum(carnum);
        perCar.setPerUserId(perCarService.findCarByNum(carnum).getPerUserId());
        perCar.setPerCarImg(path);
        perCar.setPerCarId(perCarService.findCarByNum(carnum).getPerCarId());
        perCar.setPerCarAttribute(perCarService.findCarByNum(carnum).getPerCarAttribute());
        perCarService.updateByPrimaryKey(perCar);
        return ResultMsg.success("上传车辆图片成功~", path);
    }
}
